<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="优惠卷类型" prop="couponType">
        <el-select
          v-model="queryParams.couponType"
          placeholder="请选择优惠卷类型"
          clearable
        >
          <el-option
            v-for="dict in coupon_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优惠卷名字" prop="couponName">
        <el-input
          v-model="queryParams.couponName"
          placeholder="请输入优惠卷名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="使用类型" prop="useType">
        <el-select v-model="queryParams.useType" placeholder="请选择使用类型" clearable>
          <el-option
            v-for="dict in coupon_use_range"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发布状态" prop="publish">
        <el-input
          v-model="queryParams.publish"
          placeholder="请输入发布状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"> 搜索 </el-button>
        <el-button icon="Refresh" @click="resetQuery">重置 </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['marketing:couponInfo:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['marketing:couponInfo:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="优惠卷类型" align="center" prop="couponType">
        <template #default="scope">
          <dict-tag :options="coupon_type" :value="scope.row.couponType" />
        </template>
      </el-table-column>
      <el-table-column label="优惠券图片" align="center" prop="couponImg">
        <template #default="scope">
          <image-preview :src="scope.row.couponImg" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="优惠卷名字" align="center" prop="couponName" />
      <el-table-column label="数量" align="center" prop="num" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="每人限领张数" align="center" prop="perLimit" />
      <el-table-column label="使用门槛" align="center" prop="minPoint" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180" />
      <el-table-column label="结束时间" align="center" prop="endTime" width="180"/>
      <el-table-column label="使用类型" align="center" prop="useType">
        <template #default="scope">
          <dict-tag :options="coupon_use_range" :value="scope.row.useType" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="note" />
      <el-table-column label="发行数量" align="center" prop="publishCount" />
      <el-table-column label="已使用数量" align="center" prop="useCount" />
      <el-table-column label="领取数量" align="center" prop="receiveCount" />
      <el-table-column
        label="领取的开始日期"
        align="center"
        prop="enableStartTime"
        width="180"
      />
      <el-table-column
        label="可以领取的结束日期"
        align="center"
        prop="enableEndTime"
        width="180"
      />
      <el-table-column label="优惠码" align="center" prop="code" />
      <el-table-column label="可以领取的会员等级" align="center" prop="memberLevel" />
      <el-table-column label="发布状态" align="center" prop="publish">
        <template #default="scope">
          <dict-tag :options="coupon_publish" :value="scope.row.publish" />
        </template>
      </el-table-column>
      <el-table-column label="兑换所需积分" align="center" prop="requiredIntegral" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="100"
      >
        <template #default="scope">
          <el-button
            v-if="scope.row.useType != 0"
            link
            type="primary"
            icon="Edit"
            @click="addRelation(scope.row)"
            v-hasPermi="['marketing:couponInfo:edit']"
            >添加关联</el-button
          >
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['marketing:couponInfo:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['marketing:couponInfo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label="类型" prop="couponType">
          <el-select v-model="form.couponType" placeholder="请选择优惠卷类型">
            <el-option
              v-for="dict in coupon_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="couponImg">
          <image-upload v-model="form.couponImg"></image-upload>
        </el-form-item>
        <el-form-item label="名字" prop="couponName">
          <el-input v-model="form.couponName" placeholder="请输入优惠卷名字" />
        </el-form-item>
        <el-form-item label="数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="限领张数" prop="perLimit">
          <el-input v-model="form.perLimit" placeholder="请输入每人限领张数" />
        </el-form-item>
        <el-form-item label="使用门槛" prop="minPoint">
          <el-input v-model="form.minPoint" placeholder="请输入使用门槛" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            clearable
            v-model="form.startTime"
            type="datetime"
            placeholder="请选择开始时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            clearable
            v-model="form.endTime"
            type="datetime"
            placeholder="请选择结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用类型" prop="useType">
          <el-select v-model="form.useType" placeholder="请选择使用类型">
            <el-option
              v-for="dict in coupon_use_range"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="发行数量" prop="publishCount">
          <el-input v-model="form.publishCount" placeholder="请输入发行数量" />
        </el-form-item>
        <el-form-item label="已使用数量" prop="useCount">
          <el-input v-model="form.useCount" placeholder="请输入已使用数量" />
        </el-form-item>
        <el-form-item label="领取数量" prop="receiveCount">
          <el-input v-model="form.receiveCount" placeholder="请输入领取数量" />
        </el-form-item>
        <el-form-item label="领取开始日期" prop="enableStartTime">
          <el-date-picker
            clearable
            v-model="form.enableStartTime"
            type="datetime"
            placeholder="请选择可以领取的开始日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领取结束日期" prop="enableEndTime">
          <el-date-picker
            clearable
            v-model="form.enableEndTime"
            type="datetime"
            placeholder="请选择可以领取的结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="优惠码" prop="code">
          <el-input v-model="form.code" placeholder="请输入优惠码" />
        </el-form-item>
        <el-form-item label="所需会员等级" prop="memberLevel">
          <el-input v-model="form.memberLevel" placeholder="请输入可以领取的会员等级" />
        </el-form-item>
        <el-form-item label="发布状态" prop="publish" v-show="form.id == undefined">
          <el-radio-group v-model="form.publish">
            <el-radio :label="1">发布</el-radio>
            <el-radio :label="0">下架</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="兑换所需积分" prop="requiredIntegral">
          <el-input v-model="form.requiredIntegral" placeholder="请输入兑换所需积分" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加关联 -->
    <el-dialog
      title="添加优惠券关联"
      width="450px"
      v-model="relationOpen"
      append-to-body
      :close-on-click-modal="false"
    >
      <RelationForm :coupon="form" />
    </el-dialog>
  </div>
</template>

<script setup>
import {
  listCouponInfo,
  getCouponInfo,
  delCouponInfo,
  addCouponInfo,
  updateCouponInfo,
} from "@/api/mall/server/marketing/coupon/info";
import RelationForm from "./relationForm";

const { proxy } = getCurrentInstance();
const { coupon_type, coupon_use_range, coupon_publish } = proxy.useDict(
  "coupon_type",
  "coupon_use_range",
  "coupon_publish"
);
const list = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const relationOpen = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);
const getList = async () => {
  loading.value = true;
  const res = await listCouponInfo(queryParams.value);
  list.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
// 添加关联操作
const addRelation = (row) => {
  form.value = JSON.parse(JSON.stringify(row));
  relationOpen.value = true;
};
/** 取消按钮 */
const cancel = () => {
  open.value = false;
  reset();
};
/** 表单重置 */
const reset = () => {
  form.value = {
    id: undefined,
    couponType: undefined,
    couponImg: undefined,
    couponName: undefined,
    num: undefined,
    amount: undefined,
    perLimit: undefined,
    minPoint: undefined,
    startTime: undefined,
    endTime: undefined,
    useType: undefined,
    note: undefined,
    publishCount: undefined,
    useCount: undefined,
    receiveCount: undefined,
    enableStartTime: undefined,
    enableEndTime: undefined,
    code: undefined,
    memberLevel: undefined,
    publish: undefined,
    requiredIntegral: undefined,
  };
  proxy.resetForm("formRef");
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};
/** 多选框选中数据 */
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};
/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "添加优惠券信息";
};
/**修改按钮操作 */
const handleUpdate = async (row) => {
  reset();
  const id = row.id || ids.value;
  const res = await getCouponInfo(id);
  form.value = res.data;
  open.value = true;
  title.value = "修改优惠券信息";
};
/** 提交按钮 */
const submitForm = () => {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateCouponInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCouponInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};
/** 删除按钮操作 */
const handleDelete = (row) => {
  const ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除编号为"' + ids + '"的数据项？')
    .then(function () {
      return delCouponInfo(ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};

getList();
</script>
